﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.draw;
using System.IO;
using iTextSharp.text.html.simpleparser;
using System.Net;
using System;
using System.Text;
using System.Security.Principal;
using System.Collections.Generic;
namespace dbPoll_Application.Helpers
{
    class CodeFile1 : iTextSharp.text.pdf.PdfPageEventHelper
    {
        public static bool directionUp { get; set; }
        public static int FILE { get; set; }
        public Font font = new Font(FontFactory.GetFont("Verdana", 9));
        public int totalPages { get; set; }
        public int totalfonts = FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts"); 
        public CodeFile1() { }
        public CodeFile1(int totalPages) { this.totalPages = totalPages; }
        public void CreatePDF(IList<IElement> table,String path,String type,String pollname)
        {
            MemoryStream outputStream = new MemoryStream();
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter write = PdfWriter.GetInstance(document,outputStream);
            document.Open();
            Standard(document, type, path,pollname);
            foreach (IElement element in table) document.Add(element);
            write.CloseStream=false;
            document.Close();
            outputStream.Position = 0;
            PdfReader reader = new PdfReader(outputStream);
            totalPages = reader.NumberOfPages;
            Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
            try
            {
                File.Delete(path + "/MyFirstPDF" + FILE + ".pdf");
            }
            catch (Exception e) { }
            if (FILE == int.MaxValue) directionUp = false;
            else if (FILE == 0) directionUp = true;
            if (directionUp) FILE++;
            else FILE--;
            var output = new FileStream(path+"/MyFirstPDF"+FILE+".pdf", FileMode.Create);
            PdfWriter writer = PdfWriter.GetInstance(doc, output);
            CodeFile1 pageeventhandler = new CodeFile1(totalPages);
            writer.PageEvent = pageeventhandler;
            doc.Open();
            Standard(doc, type, path,pollname);
            foreach (IElement element in table) doc.Add(element);  
            doc.Close();
        }

        public void CreatePDF2(IList<IElement> table, String path, String type)
        {
            MemoryStream outputStream = new MemoryStream();
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter write = PdfWriter.GetInstance(document, outputStream);
            document.Open();
            Standard2(document, type, path);
            foreach (IElement element in table) document.Add(element);
            write.CloseStream = false;
            document.Close();
            outputStream.Position = 0;
            PdfReader reader = new PdfReader(outputStream);
            totalPages = reader.NumberOfPages;
            Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
            try
            {
                File.Delete(path + "/MyFirstPDF" + FILE + ".pdf");
            }
            catch (Exception e) { }
            if (FILE == int.MaxValue) directionUp = false;
            else if (FILE == 0) directionUp = true;
            if (directionUp) FILE++;
            else FILE--;
            var output = new FileStream(path + "/MyFirstPDF" + FILE + ".pdf", FileMode.Create);
            PdfWriter writer = PdfWriter.GetInstance(doc, output);
            CodeFile1 pageeventhandler = new CodeFile1(totalPages);
            writer.PageEvent = pageeventhandler;
            doc.Open();
            Standard2(doc, type, path);
            foreach (IElement element in table) doc.Add(element);
            doc.Close();
        }

        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            // Add in the current page number using the "footer" font
            Paragraph para = new Paragraph("");
            //I use a PdfPtable with 2 columns to position my footer where I want it
            PdfPTable footerTbl = new PdfPTable(2);

            //set the width of the table to be the same as the document
            footerTbl.TotalWidth = document.PageSize.Width;

            //Center the table on the page
            footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

            PdfPCell cell = new PdfPCell(para);

            //set cell border to 0
            cell.Border = 0;

            //add some padding to bring away from the edge
            cell.PaddingLeft = 10;
            cell.PaddingTop = 1000;
            //add cell to table
            footerTbl.AddCell(cell);

            //create new instance of Paragraph for 2nd cell text
            para = new Paragraph("Page " + document.PageNumber+" of " + totalPages, font);

            //create new instance of cell to hold the text
            cell = new PdfPCell(para);

            //align the text to the right of the cell
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            //set border to 0
            cell.Border = 0;

            // add some padding to take away from the edge of the page
            cell.PaddingRight = 50;

            //add the cell to the table
            footerTbl.AddCell(cell);

            //write the rows out to the PDF output stream.
            footerTbl.WriteSelectedRows(0, -1, 0, 30, writer.DirectContent);

        }
        public PdfPTable SessionParticipation(String path,ViewModels.PollReportViewModel model)
        {
            PdfPTable table = new PdfPTable(4);
            table.WidthPercentage = 100;
            int[] width = new int[] { 10, 5, 5, 10 };
            table.SetWidths(width);
            Phrase phrase = new Phrase("Participant", font);
            BaseColor basecolor = new BaseColor(232, 238, 244);
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Attended");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Participated (%)");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Points Accumulated");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            foreach (var participant in model.participants)
            {
                phrase.Clear();
                phrase.Add(participant.firstName +" " + participant.lastName);
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                phrase.Add(participant.attended.ToString());
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                phrase.Add(model.participated(participant));
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                phrase.Add(model.points(participant));
                table.AddCell(new Phrase(phrase));
            }
            return table;
        }

        public PdfPTable SystemUtilization(String path, ViewModels.PollReportViewModel model)
        {
            PdfPTable table = new PdfPTable(4);
            table.WidthPercentage = 100;
            int[] width = new int[] { 1,1,1,2 };
            table.SetWidths(width);
            Phrase phrase = new Phrase("Username", font);
            BaseColor basecolor = new BaseColor(232, 238, 244);
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Access Privileges");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Date Created");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Notifications");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            foreach (var user in model.userList)
            {
                phrase.Clear();
                phrase.Add(user.UserName);
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                foreach (var role in user.Roles)
                {
                    phrase.Add(role.RoleName + "\n");
                }
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                phrase.Add(user.CreationDate.ToString());
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                if (user.Roles.Contains(new dbPoll_Application.Models.Role("Poll Administrator")) && user.AccountExpiryDate<= DateTime.Now.AddDays(30))
                {
                    phrase.Add("This account expires in " + (user.AccountExpiryDate - DateTime.Now).Days + " days.");
                }
                else phrase.Add("");
                table.AddCell(new Phrase(phrase));
            }
            return table;
        }

        public PdfPTable QuestionReport(String path, ViewModels.PollReportViewModel model,String entity)
        {
            if (model.selectedEntities.Count == 0)
            {
                PdfPTable table = new PdfPTable(4);
                int[] width = new int[4] { 2, 15, 12, 6 };
                table.WidthPercentage = 100;
                table.SetWidths(width);
                Phrase phrase = new Phrase("#", font);
                BaseColor basecolor = new BaseColor(232, 238, 244);
                BaseColor heading = new BaseColor(232, 238, 0);
                heading.Darker();
                heading.Darker();
                if (entity != null)
                {
                    PdfPCell entityline = new PdfPCell(new Phrase("Entity: " + entity, font));
                    entityline.BackgroundColor = heading;
                    entityline.Colspan = 4;
                    table.AddCell(entityline);
                }
                if (model.tick != null)
                {
                    String line;
                    if (model.include) line = "Include ";
                    else line = "Exclude ";
                    line += model.field.name + ", Values: ";
                    foreach (string i in model.tick) line += i + ", ";
                    line = line.Remove(line.Length - 2);
                    PdfPCell demo = new PdfPCell(new Phrase(line + entity, font));
                    demo.BackgroundColor = heading;
                    demo.Colspan = 4;
                    table.AddCell(demo);
                }
                table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                phrase.Clear();
                phrase.Add("Question");
                table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                phrase.Clear();
                phrase.Add("Answers");
                table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                phrase.Clear();
                phrase.Add("Number of Responses");
                table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                foreach (var question in model.questions)
                {
                    if (question.deletionTime == null)
                    {
                        phrase.Clear();
                        phrase.Add(question.questionNumber.ToString());
                        table.AddCell(new Phrase(phrase));
                        phrase.Clear();
                        phrase.Add(question.question);
                        table.AddCell(new Phrase(phrase));
                        PdfPTable options = new PdfPTable(2);
                        options.SetWidths(new int[2] { 4, 2 });
                        foreach (var answer in model.responses[question])
                        {
                            phrase.Clear();
                            phrase.Add(answer.Key);
                            options.AddCell(new Phrase(phrase));
                            phrase.Clear();
                            phrase.Add(answer.Value.ToString());
                            options.AddCell(new Phrase(phrase));
                        }

                        PdfPCell full = new PdfPCell(options);
                        full.Colspan = 2;
                        table.AddCell(full);
                    }
                }
                return table;
            }
            else
            {
                PdfPTable table = new PdfPTable(3 + model.selectedEntities.Count);
                table.WidthPercentage = 100;
                List<int> widthList = new List<int>();
                widthList.Add(2);
                widthList.Add(15);
                widthList.Add(12);
                widthList.Add(6);
                for (int i = 0; i < model.selectedEntities.Count-1; i++)
                {
                    widthList.Add(6);
                }
                table.SetWidths(widthList.ToArray());
                Phrase phrase = new Phrase("#", font);
                BaseColor basecolor = new BaseColor(232, 238, 244);
                BaseColor heading = new BaseColor(232, 238, 0);
                heading.Darker();
                heading.Darker();
                if (model.selectedEntities.Count > 0)
                {
                    Phrase entities = new Phrase("", new Font(FontFactory.GetFont("Verdana", 14)));
                    int i = 0;
                    if (model.selectedEntities.Count == 1) entities.Add("Entity: ");
                    else entities.Add("Entities: ");
                    foreach (Models.Entity ent in model.selectedEntities)
                    {
                        if (i == model.selectedEntities.Count - 1) entities.Add(ent.name);
                        else entities.Add(ent.name + ", ");
                        i++;
                    }
                    PdfPCell entityline = new PdfPCell(entities);
                    entityline.BackgroundColor = heading;
                    entityline.Colspan = 5;
                    table.AddCell(entityline);
                }
                if (model.tick != null)
                {
                    String line;
                    if (model.include) line = "Include ";
                    else line = "Exclude ";
                    line += model.field.name + ", Values: ";
                    foreach (string i in model.tick) line += i + ", ";
                    line = line.Remove(line.Length - 2);
                    PdfPCell demo = new PdfPCell(new Phrase(line + entity, font));
                    demo.BackgroundColor = heading;
                    demo.Colspan = 5;
                    table.AddCell(demo);
                }
                table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                phrase.Clear();
                phrase.Add("Question");
                table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                phrase.Clear();
                phrase.Add("Answers");
                table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                phrase.Clear();

                foreach (var e in model.selectedEntities)
                {
                    phrase.Add(e.name);
                    table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
                    phrase.Clear();
                }

                
                foreach (var question in model.questions)
                {

                    if (question.deletionTime == null)
                    {
                        phrase.Clear();
                        phrase.Add(question.questionNumber.ToString());
                        table.AddCell(new Phrase(phrase));
                        phrase.Clear();
                        phrase.Add(question.question);
                        table.AddCell(new Phrase(phrase));
                        PdfPTable options = new PdfPTable(1 + model.selectedEntities.Count);
                        List<int> widthList2 = new List<int>();
                        widthList2.Add(4);
                        widthList2.Add(2);
                        for (int i = 0; i < model.selectedEntities.Count-1; i++)
                        {
                            widthList2.Add(2);
                        }
                        options.SetWidths(widthList2.ToArray());
                        
                            foreach (var answer in model.filteredResponses[question])
                            {
                                phrase.Clear();
                                phrase.Add(answer.Key);
                                options.AddCell(new Phrase(phrase));
                                foreach (var e in model.selectedEntities)
                                {


                                phrase.Clear();
                                phrase.Add(answer.Value[e.entityID].ToString());
                                options.AddCell(new Phrase(phrase));
                            

                                }

                            }
                        PdfPCell full = new PdfPCell(options);
                        full.Colspan = model.selectedEntities.Count+1;
                        table.AddCell(full);
                    }
                }
                return table;
            }
        }

        public PdfPTable GraphicalReport(String path, Models.Poll poll, String urls,ViewModels.PollReportViewModel model,String entity)
        {
            int totalfonts = FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");
            BaseColor basecolor = new BaseColor(232, 238, 244);
            String[] url = urls.Split(',');
            PdfPTable table = new PdfPTable(1);
            table.WidthPercentage = 100;
            if (model.selectedEntities.Count > 0)
            {
                Phrase entities = new Phrase("", new Font(FontFactory.GetFont("Verdana", 14)));
                int i = 0;
                if (model.selectedEntities.Count == 1) entities.Add("Entity: ");
                else entities.Add("Entities: ");
                foreach (Models.Entity ent in model.selectedEntities)
                {
                    if (i==model.selectedEntities.Count-1) entities.Add(ent.name);
                    else entities.Add(ent.name + ", ");
                    i++;
                }
                table.AddCell(new PdfPCell(entities) { BackgroundColor = new BaseColor(232, 238, 0) });
            }
            if (model.tick != null && model.field!=null)
            {
                String line;
                if (model.include) line = "Include ";
                else line = "Exclude ";
                line += model.field.name + ", Values: ";
                foreach (string i in model.tick) line += i + ", ";
                line = line.Remove(line.Length - 2);
                PdfPCell demo = new PdfPCell(new PdfPCell(new Phrase(line + entity, new Font(FontFactory.GetFont("Verdana", 14)))) { BackgroundColor = new BaseColor(232, 238, 0) });
                table.AddCell(demo);
            }
            foreach (var question in poll.questions)
            {
                PdfPTable ques = new PdfPTable(2);
                Paragraph heading = new Paragraph("Question " + question.questionNumber + ": " + question.question, new Font(FontFactory.GetFont("Verdana", 14)));
                PdfPCell full = new PdfPCell(heading);
                full.Colspan = 2;
                full.BackgroundColor = basecolor;
                ques.AddCell(full);
                Image jpg = Image.GetInstance(new Uri("http:" + url.GetValue(question.questionNumber - 1)));
                jpg.SpacingAfter = 12f;
                jpg.SpacingBefore = 12f;
                jpg.Alignment = Element.ALIGN_LEFT | Image.TEXTWRAP;
                ques.SetWidths(new float[2]{float.Parse(jpg.Width.ToString()), float.Parse((660 - jpg.Width).ToString())});
                ques.AddCell(jpg);
                PdfPCell options = new PdfPCell();
                options.Padding=20f;
                if (model.selectedEntities.Count > 0)
                {
                    if (question.GetType().IsSubclassOf(typeof(Models.MultipleChoiceQuestion)))
                    {
                        int i = 0;
                        foreach (var e in model.selectedEntities)
                        {
                            foreach (var option in ((Models.MultipleChoiceQuestion)question).options)
                            {
                                if (option.correctCurrent) options.AddElement(new Paragraph(i + 1 + ". " + option.answer + ": " + e.name + " (Correct)", new Font(FontFactory.GetFont("Verdana", 12))));
                                else options.AddElement(new Paragraph(i + 1 + ". " + option.answer + ": " + e.name, new Font(FontFactory.GetFont("Verdana", 12))));
                                i++;
                            }
                        }
                    }
                    else if (question.GetType().IsSubclassOf(typeof(Models.ShortResponseQuestion)))
                    {
                        int i = 0;
                        foreach (var e in model.selectedEntities)
                        {
                            ICollection<String> keys = model.responses[question].Keys;
                            foreach (var key in keys)
                            {
                                options.AddElement(new Paragraph(i + 1 + ". " + key + ": " + e.name, new Font(FontFactory.GetFont("Verdana", 12))));
                                i++;
                            }
                        }
                    }
                }
                else
                {
                    if (question.GetType().IsSubclassOf(typeof(Models.MultipleChoiceQuestion)))
                    {
                        int i = 0;
                        foreach (var option in ((Models.MultipleChoiceQuestion)question).options)
                        {
                            if (option.correctCurrent) options.AddElement(new Paragraph(i + 1 + ". " + option.answer + " (Correct)", new Font(FontFactory.GetFont("Verdana", 12))));
                            else options.AddElement(new Paragraph(i + 1 + ". " + option.answer, new Font(FontFactory.GetFont("Verdana", 12))));
                            i++;
                        }
                    }
                    else if (question.GetType().IsSubclassOf(typeof(Models.ShortResponseQuestion)))
                    {
                        int i = 0;
                        ICollection<String> keys = model.responses[question].Keys;
                        foreach (var key in keys)
                        {
                            options.AddElement(new Paragraph(i + 1 + ". " + key, new Font(FontFactory.GetFont("Verdana", 12))));
                            i++;
                        }
                    }
                }
                ques.AddCell(options);
                table.AddCell(new PdfPCell(ques));
            }
            return table;
        }
        public PdfPTable SessionHistory(IList<Models.Poll> polls, String locations)
        {
            string[] location = locations.Split('|');
            PdfPTable table = new PdfPTable(8);
            table.WidthPercentage = 100;
            int[] width = new int[] { 3,8,6,14,6,6,8,8 };
            table.SetWidths(width);
            Phrase phrase = new Phrase("Poll #", font);
            BaseColor basecolor = new BaseColor(232, 238, 244);
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Poll Name");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Attendance");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Location");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Opening Time");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Closing Time");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Poll Masters");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            phrase.Clear();
            phrase.Add("Poll Creators");
            table.AddCell(new PdfPCell(new PdfPCell(new Phrase(phrase)) { BackgroundColor = basecolor }));
            var i = 0;
            foreach (var poll in polls)
            {
                phrase.Clear();
                phrase.Add(poll.pollID.ToString());
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                phrase.Add(poll.name);
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                int attendance = 0;
                foreach (var participant in poll.participants)
                {
                    if (participant.attended)
                    {
                        attendance += 1;
                    }
                }
                phrase.Add(attendance.ToString());
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                phrase.Add(location[i]);
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                if (poll.pollStart.HasValue) phrase.Add(poll.pollStart.Value.ToString());
                else phrase.Add("");
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                phrase.Add(poll.pollClose.ToString());
                table.AddCell(new Phrase(phrase));
                phrase.Clear();
                PdfPCell masters = new PdfPCell();
                foreach (Models.User master in poll.pollMasters) masters.AddElement(new Paragraph(master.FirstName + " " + master.LastName, font));
                table.AddCell(masters);
                PdfPCell creators = new PdfPCell();
                creators.BorderWidthLeft = 0;
                creators.BorderWidthTop = 0;
                foreach (Models.User creator in Models.UserRepository.GetCreatorsOfPoll(poll)) creators.AddElement(new Paragraph(creator.FirstName + " " + creator.LastName, font));
                table.AddCell(creators);
                i++;
            }
            return table;
        }
        private void Standard(Document doc, String type, String path, String pollname)
        {
            Image logo = Image.GetInstance(path + "/dbPollLogoBlack.png");
            logo.ScaleToFit(100, 200);
            Paragraph title = new Paragraph(type, new Font(FontFactory.GetFont("Verdana", 16)));
            DateTime now = DateTime.Now;
            Paragraph date = new Paragraph("Created: " + now.Day + "/" + now.Month + "/" + now.Year);
            date.Alignment = Element.ALIGN_RIGHT;
            doc.Add(logo);
            doc.Add(title);
            Paragraph poll = new Paragraph(pollname, new Font(FontFactory.GetFont("Verdana", 16)));
            doc.Add(poll);
            doc.Add(date);
            doc.Add(new Paragraph(Environment.NewLine));
            doc.Add(new LineSeparator());
            doc.Add(new Paragraph(Environment.NewLine));
        }

        private void Standard2(Document doc, String type, String path)
        {
            Image logo = Image.GetInstance(path + "/dbPollLogoBlack.png");
            logo.ScaleToFit(100, 200);
            Paragraph title = new Paragraph(type, new Font(FontFactory.GetFont("Verdana", 16)));
            DateTime now = DateTime.Now;
            Paragraph date = new Paragraph("Created: " + now.Day + "/" + now.Month + "/" + now.Year);
            date.Alignment = Element.ALIGN_RIGHT;
            doc.Add(logo);
            doc.Add(title);
            doc.Add(date);
            doc.Add(new Paragraph(Environment.NewLine));
            doc.Add(new LineSeparator());
            doc.Add(new Paragraph(Environment.NewLine));
        }
    }
}